package file;


import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 实现文件的复制
 * User: 王圆豪
 * Date: 2025-05-12
 * Time: 19:26
 */

public class Demo13 {
    public static void main(String[] args) {
        System.out.println("请输入源文件路径：");
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        System.out.println("请输入目标文件路径：");
        String s2 = sc.next();

        File file1 = new File(s1);
        File file2 = new File(s2);
        if (!file1.isFile()) {
            System.out.println("该路径下没有或不是文件！");
            return;
        }

        if (!file2.getParentFile().isDirectory()){
            System.out.println("目标文件所在目录不存在！");
        }

        //创建流对象
        try (InputStream inputStream = new FileInputStream(file1);
             OutputStream outputStream = new FileOutputStream(file2)) {
            while (true) {
                byte[] data = new byte[1024];
                int n = inputStream.read(data);
                if (n == -1) break;
                outputStream.write(data, 0, n);
            }
            System.out.println("复制成功！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
